package com.robosense.datacenter.exception;


import com.robosense.datacenter.result.ResponseVO;
import com.robosense.datacenter.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    /**
     * 默认通用异常处理
     *
     * @param request  请求
     * @param response 响应
     * @param e        异常
     * @return 返回封装信息
     */
    @ExceptionHandler()
    public ResponseVO defaultExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
        log.error("=exception=" + e.getMessage());
        e.printStackTrace();
        return R.vo(R.ResultType.FAILED);
    }

    /**
     * 自定义异常处理
     *
     * @param e 异常
     * @return 返回响应信息
     */
    @ExceptionHandler(RuntimeException.class)
    public ResponseVO exceptionHandler(RuntimeException e) {
        e.printStackTrace();
        log.error("=RuntimeException=" + e.getMessage());
        return R.vo(R.ResultType.FAILED);
    }
}
